<!DOCTYPE html>
<html>
<head>
    <title>综合JavaScript接口测试</title>
    <meta charset="utf-8" />
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; line-height: 1.6; }
        .test-section { margin: 20px 0; padding: 15px; border: 1px solid #ddd; border-radius: 8px; }
        .test-result { margin: 10px 0; padding: 10px; border-radius: 5px; }
        .success { background: #d4edda; color: #155724; border: 1px solid #c3e6cb; }
        .error { background: #f8d7da; color: #721c24; border: 1px solid #f5c6cb; }
        .warning { background: #fff3cd; color: #856404; border: 1px solid #ffeaa7; }
        .info { background: #d1ecf1; color: #0c5460; border: 1px solid #bee5eb; }
        button { background: #007bff; color: white; border: none; padding: 8px 16px; border-radius: 5px; cursor: pointer; margin: 5px; }
        button:hover { background: #0056b3; }
        button:disabled { background: #6c757d; cursor: not-allowed; }
        pre { background: #f8f9fa; padding: 10px; border-radius: 5px; overflow-x: auto; font-size: 12px; }
        .test-controls { margin: 15px 0; }
        .interface-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 20px; margin: 20px 0; }
        .interface-card { border: 1px solid #ddd; border-radius: 8px; padding: 15px; }
        .interface-card h3 { margin-top: 0; color: #007bff; }
        .method-list { margin: 10px 0; }
        .method-item { padding: 5px 0; border-bottom: 1px solid #eee; }
        .method-item:last-child { border-bottom: none; }
    </style>
</head>
<body>
    <h1>🎵 KiteMusic JavaScript接口综合测试</h1>

    <div class="test-section">
        <h2>📋 测试总览</h2>
        <div id="test-overview"></div>
        <div class="test-controls">
            <button onclick="runAllTests()">运行所有测试</button>
            <button onclick="resetTests()">重置测试结果</button>
            <button onclick="showInterfaceOverview()">显示接口概览</button>
        </div>
    </div>

    <div class="test-section">
        <h2>🔧 FileUtil 测试</h2>
        <button onclick="testFileUtil()">测试FileUtil</button>
        <div id="fileutil-results"></div>
    </div>

    <div class="test-section">
        <h2>👁️ ViewUtil 测试</h2>
        <button onclick="testViewUtil()">测试ViewUtil</button>
        <div id="viewutil-results"></div>
    </div>

    <div class="test-section">
        <h2>🌐 NetUtil 测试</h2>
        <button onclick="testNetUtil()">测试NetUtil</button>
        <div id="netutil-results"></div>
    </div>

    <div class="test-section">
        <h2>🎵 AudioUtil 测试 (已禁用)</h2>
        <p>AudioUtil模块已开发完成但暂时禁用。如需测试，请取消MainActivity中的注释。</p>
        <button onclick="testAudioUtil()" disabled>测试AudioUtil (已禁用)</button>
        <div id="audioutil-results"></div>
    </div>

    <div class="test-section">
        <h2>💾 CacheEngine 测试</h2>
        <button onclick="testCacheEngine()">测试CacheEngine</button>
        <div id="cacheengine-results"></div>
    </div>

    <div class="test-section">
        <h2>📊 综合统计</h2>
        <div id="statistics"></div>
    </div>

    <!-- 接口概览模态框 -->
    <div id="interfaceModal" style="display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.5); z-index: 1000;">
        <div style="position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); background: white; padding: 20px; border-radius: 10px; max-width: 90%; max-height: 80%; overflow-y: auto;">
            <h2>📚 JavaScript接口概览</h2>
            <div class="interface-grid" id="interfaceOverview"></div>
            <button onclick="closeModal()" style="margin-top: 20px;">关闭</button>
        </div>
    </div>

    <script>
        // 测试统计
        let testStats = {
            total: 0,
            passed: 0,
            failed: 0,
            errors: 0
        };

        // 测试结果容器
        function addTestResult(containerId, message, type = 'info') {
            const container = document.getElementById(containerId);
            const div = document.createElement('div');
            div.className = `test-result ${type}`;
            div.innerHTML = `<strong>${new Date().toLocaleTimeString()}</strong> - ${message}`;
            container.appendChild(div);

            // 更新统计
            testStats.total++;
            if (type === 'success') testStats.passed++;
            else if (type === 'error') testStats.errors++;
            else testStats.failed++;

            updateStatistics();
        }

        function updateStatistics() {
            const statsDiv = document.getElementById('statistics');
            statsDiv.innerHTML = `
                <div class="test-result info">
                    <strong>测试统计:</strong><br>
                    总测试数: ${testStats.total}<br>
                    通过: ${testStats.passed}<br>
                    失败: ${testStats.failed}<br>
                    异常: ${testStats.errors}<br>
                    成功率: ${testStats.total > 0 ? Math.round((testStats.passed / testStats.total) * 100) : 0}%
                </div>
            `;
        }

        function resetTests() {
            testStats = { total: 0, passed: 0, failed: 0, errors: 0 };
            document.querySelectorAll('.test-result').forEach(el => el.remove());
            updateStatistics();
            addTestResult('test-overview', '测试结果已重置', 'info');
        }

        // ============ FileUtil 测试 ============
        async function testFileUtil() {
            addTestResult('fileutil-results', '开始测试FileUtil...', 'info');

            try {
                // 测试文件创建
                let createResult = FileUtil.createFile('internal', 'test_file', '.txt');
                let createData = JSON.parse(createResult);

                if (createData.success) {
                    addTestResult('fileutil-results', `✅ 文件创建成功: ${createData.data}`, 'success');

                    let filePath = createData.data;

                    // 测试文件写入
                    let writeResult = FileUtil.writeTextToFile(filePath, '测试内容 Test Content');
                    let writeData = JSON.parse(writeResult);

                    if (writeData.success) {
                        addTestResult('fileutil-results', '✅ 文件写入成功', 'success');

                        // 测试文件读取
                        let readResult = FileUtil.readTextFromFile(filePath);
                        let readData = JSON.parse(readResult);

                        if (readData.success && readData.data.includes('测试内容')) {
                            addTestResult('fileutil-results', '✅ 文件读取成功', 'success');
                        } else {
                            addTestResult('fileutil-results', '❌ 文件读取失败', 'error');
                        }

                        // 测试文件追加
                        let appendResult = FileUtil.appendTextToFile(filePath, '\n追加内容');
                        let appendData = JSON.parse(appendResult);

                        if (appendData.success) {
                            addTestResult('fileutil-results', '✅ 文件追加成功', 'success');
                        } else {
                            addTestResult('fileutil-results', '❌ 文件追加失败', 'error');
                        }

                        // 测试文件信息
                        let sizeResult = FileUtil.getFileSizeByPath(filePath);
                        let sizeData = JSON.parse(sizeResult);

                        if (sizeData.success) {
                            addTestResult('fileutil-results', `✅ 文件大小获取成功: ${sizeData.data}字节`, 'success');
                        } else {
                            addTestResult('fileutil-results', '❌ 文件大小获取失败', 'error');
                        }

                        // 测试文件存在性
                        let existsResult = FileUtil.fileExistsByPath(filePath);
                        let existsData = JSON.parse(existsResult);

                        if (existsData.success && existsData.data === 'true') {
                            addTestResult('fileutil-results', '✅ 文件存在性检查成功', 'success');
                        } else {
                            addTestResult('fileutil-results', '❌ 文件存在性检查失败', 'error');
                        }

                        // 清理测试文件
                        let deleteResult = FileUtil.deleteFileByPath(filePath);
                        let deleteData = JSON.parse(deleteResult);

                        if (deleteData.success) {
                            addTestResult('fileutil-results', '✅ 测试文件清理成功', 'success');
                        } else {
                            addTestResult('fileutil-results', '❌ 测试文件清理失败', 'error');
                        }

                    } else {
                        addTestResult('fileutil-results', '❌ 文件写入失败: ' + writeData.error, 'error');
                    }

                } else {
                    addTestResult('fileutil-results', '❌ 文件创建失败: ' + createData.error, 'error');
                }

                addTestResult('fileutil-results', '🎉 FileUtil测试完成', 'success');

            } catch (e) {
                addTestResult('fileutil-results', '❌ FileUtil测试异常: ' + e.message, 'error');
            }
        }

        // ============ ViewUtil 测试 ============
        async function testViewUtil() {
            addTestResult('viewutil-results', '开始测试ViewUtil...', 'info');

            try {
                // 测试Toast
                let toastResult = ViewUtil.showToastWithCallback('ViewUtil测试消息', 'toastCallback');
                let toastData = JSON.parse(toastResult);

                if (toastData.success) {
                    addTestResult('viewutil-results', '✅ Toast显示成功', 'success');
                } else {
                    addTestResult('viewutil-results', '❌ Toast显示失败: ' + toastData.error, 'error');
                }

                // 测试对话框管理
                let dialogsResult = ViewUtil.getActiveDialogs();
                let dialogsData = JSON.parse(dialogsResult);

                if (dialogsData.success) {
                    addTestResult('viewutil-results', `✅ 对话框管理正常，活跃对话框: ${JSON.stringify(dialogsData.data)}`, 'success');
                } else {
                    addTestResult('viewutil-results', '❌ 对话框管理失败: ' + dialogsData.error, 'error');
                }

                addTestResult('viewutil-results', '🎉 ViewUtil测试完成', 'success');

            } catch (e) {
                addTestResult('viewutil-results', '❌ ViewUtil测试异常: ' + e.message, 'error');
            }
        }

        // ============ NetUtil 测试 ============
        async function testNetUtil() {
            addTestResult('netutil-results', '开始测试NetUtil...', 'info');

            try {
                // 测试网络状态
                let networkAvailable = NetUtil.isNetworkAvailable();
                addTestResult('netutil-results', `📡 网络状态: ${networkAvailable ? '✅ 可用' : '❌ 不可用'}`, networkAvailable ? 'success' : 'warning');

                // 测试缓存统计
                let cacheStatsResult = NetUtil.getCacheStats();
                let cacheStatsData = JSON.parse(cacheStatsResult);

                if (cacheStatsData.cacheSize !== undefined) {
                    addTestResult('netutil-results', `✅ 缓存统计获取成功: ${cacheStatsData.cacheSize}项缓存`, 'success');
                } else {
                    addTestResult('netutil-results', '❌ 缓存统计获取失败: ' + cacheStatsData.error, 'error');
                }

                addTestResult('netutil-results', '🎉 NetUtil测试完成', 'success');

            } catch (e) {
                addTestResult('netutil-results', '❌ NetUtil测试异常: ' + e.message, 'error');
            }
        }

        // ============ AudioUtil 测试 ============
        async function testAudioUtil() {
            addTestResult('audioutil-results', 'AudioUtil模块已禁用', 'warning');
            addTestResult('audioutil-results', '如需测试音频处理功能，请取消MainActivity中的注释', 'info');
            addTestResult('audioutil-results', '启用方法：取消第39行和第93行的注释', 'info');
        }

        // ============ CacheEngine 测试 ============
        async function testCacheEngine() {
            addTestResult('cacheengine-results', '开始测试CacheEngine...', 'info');

            try {
                // 测试基本功能
                let testResult = CacheEngine.testCache();
                let testData = JSON.parse(testResult);

                if (testData.success) {
                    addTestResult('cacheengine-results', '✅ CacheEngine基本测试通过', 'success');
                } else {
                    addTestResult('cacheengine-results', '❌ CacheEngine基本测试失败: ' + testData.error, 'error');
                }

                // 测试缓存添加
                let addResult = CacheEngine.addCache('测试歌曲', '测试歌手', 'cover.jpg', 'music.mp3', '歌词', 'L1');
                let addData = JSON.parse(addResult);

                if (addData.success) {
                    addTestResult('cacheengine-results', `✅ 缓存添加成功: ID=${addData.data.id}`, 'success');

                    // 测试缓存获取
                    let getResult = CacheEngine.getCache(addData.data.id);
                    let getData = JSON.parse(getResult);

                    if (getData.success) {
                        addTestResult('cacheengine-results', `✅ 缓存获取成功: ${getData.data.musicName}`, 'success');
                    } else {
                        addTestResult('cacheengine-results', '❌ 缓存获取失败: ' + getData.error, 'error');
                    }

                    // 测试缓存查找
                    let findResult = CacheEngine.findCache('测试歌曲', '测试歌手');
                    let findData = JSON.parse(findResult);

                    if (findData.success) {
                        addTestResult('cacheengine-results', `✅ 缓存查找成功: ${findData.data.musicName}`, 'success');
                    } else {
                        addTestResult('cacheengine-results', '❌ 缓存查找失败: ' + findData.error, 'error');
                    }

                } else {
                    addTestResult('cacheengine-results', '❌ 缓存添加失败: ' + addData.error, 'error');
                }

                // 测试缓存统计
                let statsResult = CacheEngine.getCacheStats();
                let statsData = JSON.parse(statsResult);

                if (statsData.success) {
                    addTestResult('cacheengine-results', `✅ 缓存统计获取成功: 总计${statsData.data.total}项`, 'success');
                } else {
                    addTestResult('cacheengine-results', '❌ 缓存统计获取失败: ' + statsData.error, 'error');
                }

                addTestResult('cacheengine-results', '🎉 CacheEngine测试完成', 'success');

            } catch (e) {
                addTestResult('cacheengine-results', '❌ CacheEngine测试异常: ' + e.message, 'error');
            }
        }

        // ============ 综合测试 ============
        async function runAllTests() {
            addTestResult('test-overview', '🚀 开始运行所有测试...', 'info');

            try {
                await testFileUtil();
                await new Promise(resolve => setTimeout(resolve, 1000)); // 短暂延迟

                await testViewUtil();
                await new Promise(resolve => setTimeout(resolve, 1000));

                await testNetUtil();
                await new Promise(resolve => setTimeout(resolve, 1000));

                await testAudioUtil();
                await new Promise(resolve => setTimeout(resolve, 1000));

                await testCacheEngine();

                addTestResult('test-overview', '🎉 所有测试完成！', 'success');

            } catch (e) {
                addTestResult('test-overview', '❌ 综合测试异常: ' + e.message, 'error');
            }
        }

        // ============ 接口概览 ============
        function showInterfaceOverview() {
            const modal = document.getElementById('interfaceModal');
            const overview = document.getElementById('interfaceOverview');

            overview.innerHTML = `
                <div class="interface-card">
                    <h3>💾 FileUtil (文件操作)</h3>
                    <div class="method-list">
                        <div class="method-item">✅ createFile() - 创建文件</div>
                        <div class="method-item">✅ writeTextToFile() - 写入文件</div>
                        <div class="method-item">✅ readTextFromFile() - 读取文件</div>
                        <div class="method-item">✅ appendTextToFile() - 追加文件</div>
                        <div class="method-item">✅ deleteFileByPath() - 删除文件</div>
                        <div class="method-item">✅ fileExistsByPath() - 检查文件存在</div>
                        <div class="method-item">✅ getFileSizeByPath() - 获取文件大小</div>
                        <div class="method-item">✅ renameFileByPath() - 重命名文件</div>
                        <div class="method-item">✅ copyFileByPath() - 复制文件</div>
                        <div class="method-item">✅ listDirectory() - 列出目录</div>
                        <div class="method-item">✅ createDirectoryByPath() - 创建目录</div>
                        <div class="method-item">✅ getStorageInfo() - 存储信息</div>
                        <div class="method-item">✅ cleanupTempFiles() - 清理临时文件</div>
                    </div>
                </div>

                <div class="interface-card">
                    <h3>👁️ ViewUtil (视图交互)</h3>
                    <div class="method-list">
                        <div class="method-item">✅ showAlertDialogWithCallback() - 提示对话框</div>
                        <div class="method-item">✅ showConfirmDialogWithCallback() - 确认对话框</div>
                        <div class="method-item">✅ showListDialogWithCallback() - 列表对话框</div>
                        <div class="method-item">✅ showSingleChoiceDialogWithCallback() - 单选对话框</div>
                        <div class="method-item">✅ showInputDialog() - 输入对话框</div>
                        <div class="method-item">✅ showProgressDialogWithCallback() - 进度对话框</div>
                        <div class="method-item">✅ showToastWithCallback() - Toast提示</div>
                        <div class="method-item">✅ dismissDialog() - 关闭对话框</div>
                        <div class="method-item">✅ getActiveDialogs() - 获取活跃对话框</div>
                        <div class="method-item">✅ cleanupDialogs() - 清理对话框</div>
                    </div>
                </div>

                <div class="interface-card">
                    <h3>🌐 NetUtil (网络请求)</h3>
                    <div class="method-list">
                        <div class="method-item">✅ getAsync() - 异步GET请求</div>
                        <div class="method-item">✅ getSync() - 同步GET请求</div>
                        <div class="method-item">✅ getCachedAsync() - 带缓存异步GET</div>
                        <div class="method-item">✅ getCachedSync() - 带缓存同步GET</div>
                        <div class="method-item">✅ postAsync() - 异步POST请求</div>
                        <div class="method-item">✅ headAsync() - 异步HEAD请求</div>
                        <div class="method-item">✅ isNetworkAvailable() - 网络状态</div>
                        <div class="method-item">✅ getCacheStats() - 缓存统计</div>
                        <div class="method-item">✅ clearCache() - 清理缓存</div>
                        <div class="method-item">✅ getCacheDetails() - 缓存详情</div>
                    </div>
                </div>

                <div class="interface-card">
                    <h3>🎵 AudioUtil (音频处理) - 已禁用</h3>
                    <p style="color: #666; font-size: 14px;">AudioUtil模块已开发完成但暂时禁用。如需使用，请取消MainActivity中的注释。</p>
                    <div class="method-list">
                        <div class="method-item" style="color: #999;">⏸️ getAudioInfo() - 音频元数据</div>
                        <div class="method-item" style="color: #999;">⏸️ getAudioDuration() - 音频时长</div>
                        <div class="method-item" style="color: #999;">⏸️ setEqualizerPreset() - 设置均衡器预设</div>
                        <div class="method-item" style="color: #999;">⏸️ getEqualizerInfo() - 均衡器信息</div>
                        <div class="method-item" style="color: #999;">⏸️ setEqualizerBands() - 设置均衡器频段</div>
                        <div class="method-item" style="color: #999;">⏸️ setEqualizerEnabled() - 启用均衡器</div>
                        <div class="method-item" style="color: #999;">⏸️ getAudioWaveform() - 音频波形</div>
                        <div class="method-item" style="color: #999;">⏸️ convertAudioFormat() - 格式转换</div>
                    </div>
                </div>

                <div class="interface-card">
                    <h3>💾 CacheEngine (缓存引擎)</h3>
                    <div class="method-list">
                        <div class="method-item">✅ addCache() - 添加缓存</div>
                        <div class="method-item">✅ getCache() - 获取缓存</div>
                        <div class="method-item">✅ findCache() - 查找缓存</div>
                        <div class="method-item">✅ getCacheStats() - 缓存统计</div>
                        <div class="method-item">✅ cleanupCache() - 清理缓存</div>
                        <div class="method-item">✅ clearAllCache() - 清空缓存</div>
                        <div class="method-item">✅ testCache() - 测试功能</div>
                    </div>
                </div>
            `;

            modal.style.display = 'block';
        }

        function closeModal() {
            document.getElementById('interfaceModal').style.display = 'none';
        }

        // 页面加载完成后初始化
        document.addEventListener('DOMContentLoaded', function() {
            addTestResult('test-overview', '🚀 JavaScript接口测试页面已加载', 'info');
            addTestResult('test-overview', '📋 点击上方按钮开始测试各个模块', 'info');
            addTestResult('test-overview', '🎯 所有接口都已暴露给JavaScript使用', 'success');
        });

        // 点击模态框外部关闭
        document.getElementById('interfaceModal').addEventListener('click', function(e) {
            if (e.target === this) {
                closeModal();
            }
        });
    </script>
</body>
</html>
